import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    @o firstRequest = false;
    @o rolesList = [];


    will() {

    }

    addRoleMethod() {
        this.stores.RolesDialog.fire('rolesDialog:show');
    }

    getRolesList(loading) {
        loading && this.stores.Frame.showLoading();
        request({
            url: $$.api('getRoleList')
        }).then(res => {
            this.rolesList = res.data;
            this.firstRequest = true;
            this.stores.Frame.hideLoading();
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.showMessage(2, err.message);
        })
    }

    editClick(item) {
        this.stores.RolesDialog.fire('rolesDialog:show', item);
    }

    deleteClick(item) {
        this.stores.ConfirmDialog.fire('confirmDialog:show', {
            tips: item.state === $$.SEM.ROLE_ACTIVE_STATE ? '是否要冻结该身份?' : '是否要解冻该身份?',
            func: () => {
                if (item.state === $$.SEM.ROLE_ACTIVE_STATE) {
                    this.stores.Frame.showLoading();
                    request({
                        url: $$.api('roleDelete') + item.id,
                        method: 'delete'
                    }).then(res => {
                        this.getRolesList();
                        this.stores.Message.showMessage(1, res.message);
                    }).catch(err => {
                        this.stores.Frame.hideLoading();
                        this.stores.Message.showMessage(2, err.message);
                    });
                } else {
                    this.stores.Frame.showLoading();
                    request({
                        url: $$.api('roleRestore') + item.id,
                        method: 'put',
                    }).then(res => {
                        this.getRolesList();
                        this.stores.Message.showMessage(1, res.message);
                    }).catch(err => {
                        this.stores.Frame.hideLoading();
                        this.stores.Message.showMessage(2, err.message);
                    });
                }
            }
        })
    }

    mount() {
        this.getRolesList();
        this.stores.RolesDialog.on('rolesDialog:update', () => {
            this.getRolesList(true);
        });
    }

}

export default Store;